package com.ssyc.gsk_teacher.activity;

import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ssyc.common.adapter.VpCommonAdapter;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.Constants;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.utils.SnackbarUtil;
import com.ssyc.common.view.NoScrollViewPager;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.bean.ClassInfo;
import com.ssyc.gsk_teacher.fragment.TeacherEmpiricalValueFragment;
import com.ssyc.gsk_teacher.fragment.TeacherGoldFragment;
import com.ssyc.gsk_teacher.fragment.TeacherPetLevelFragment;
import com.ssyc.gsk_teacher.fragment.TeacherPushCardFragment;
import com.ssyc.gsk_teacher.utils.ClassPoputil;
import com.ssyc.gsk_teacher.utils.ClassUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 排行榜
 * Created by Administrator on 2018/7/4 0004.
 */

public class TeacherTakeLineListActivity extends BaseActivity implements View.OnClickListener {
    private ImageView ivBack;
    /**
     * 排 行 榜
     */
    private TextView tvStTitle;
    private ImageView ivChooseClass;
    private LinearLayout llTitle;
    /**
     * 宠物等级
     */
    private TextView tvCwdj;
    /**
     * 经验值
     */
    private TextView tvJyz;
    /**
     * 金币
     */
    private TextView tvJb;
    /**
     * 打卡
     */
    private TextView tvDk;
    private NoScrollViewPager vp;
    private LinearLayout llContent;
    private RelativeLayout rlLoading;


    //所有班级的列表
    private List<ClassInfo.ListBean> classInfos;

    //记录弹出展示班级列表PopUpWindow的选中位置的值,默认第一个被选中
    private int popSelectedPos = 0;

    //是否获取班级列表数据完成的标识,没完成之前点击选择班级的弹框提示正在加载,不允许点击
    private boolean isLoadClassCompleted;

    //填充ViewPager的Fragment集合
    private List<Fragment> fragments;

    //通知四个Fragment子界面进行刷新的标识
    public static final int UPDATE_REFRESH = 0x440;

    @Override

    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_take_line_list;
    }

    @Override
    protected void init() {

        showContent();

        //初始化视图
        initView();

        //请求班级数据
        httpClass();

    }

    /**
     * 初始化视图
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        tvStTitle = findViewById(R.id.tv_st_title);
        ivChooseClass = findViewById(R.id.iv_choose_class);
        ivChooseClass.setOnClickListener(this);
        llTitle = findViewById(R.id.ll_title);
        tvCwdj = findViewById(R.id.tv_cwdj);
        tvCwdj.setOnClickListener(this);
        tvJyz = findViewById(R.id.tv_jyz);
        tvJyz.setOnClickListener(this);
        tvJb = findViewById(R.id.tv_jb);
        tvJb.setOnClickListener(this);
        tvDk = findViewById(R.id.tv_dk);
        tvDk.setOnClickListener(this);
        vp = findViewById(R.id.vp);
        llContent = findViewById(R.id.ll_content);
        llContent.setVisibility(View.GONE);
        rlLoading = findViewById(R.id.rl_loading);
        rlLoading.setVisibility(View.VISIBLE);
    }


    /**
     * 请求班级里列表数据
     */
    private void httpClass() {
        ClassUtil.httpClass(rlLoading, this, new ClassUtil.onSuccessListener() {
            @Override
            public void onSuccess(List<ClassInfo.ListBean> datas) {

                if (null != rlLoading && rlLoading.getVisibility() != View.GONE) {
                    rlLoading.setVisibility(View.GONE);
                }

                if (llContent != null && llContent.getVisibility() != View.VISIBLE) {
                    llContent.setVisibility(View.VISIBLE);
                }

                //回调班级数据
                classInfos = datas;

                //改变加载完成的标记
                isLoadClassCompleted = true;

                //设置数据
                initVp();
            }

            @Override
            public void hasNoData() {
                ivChooseClass.setVisibility(View.INVISIBLE);
            }
        });
    }


    /**
     * 设置Vp
     */
    private void initVp() {
        fragments = new ArrayList<>();
        //添加四个Fragment
        fragments.add(TeacherPetLevelFragment.newInstance(classInfos.get(popSelectedPos).getClassnum()));
        fragments.add(TeacherEmpiricalValueFragment.newInstance(classInfos.get(popSelectedPos).getClassnum()));
        fragments.add(TeacherGoldFragment.newInstance(classInfos.get(popSelectedPos).getClassnum()));
        fragments.add(TeacherPushCardFragment.newInstance(classInfos.get(popSelectedPos).getClassnum()));
        //设置Vp
        VpCommonAdapter adapter = new VpCommonAdapter(getSupportFragmentManager(), fragments);
        vp.setOffscreenPageLimit(fragments.size());
        vp.setPagerEnabled(false);
        vp.setAdapter(adapter);
    }

    /**
     * 弹出展示班级列表的方法
     */
    private void showClassPop() {
        ClassPoputil.showClassPop(this, classInfos, popSelectedPos, llTitle, new ClassPoputil.onPopItemClickListener() {
            @Override
            public void onItemClick(int pos) {
                //记录当前点击条目的位置
                popSelectedPos = pos;
                //通过EventBus通知对应的四个Fragment进行数据刷新
                BusInfo busInfo = new BusInfo();
                busInfo.setType(UPDATE_REFRESH);
                //将选择的班级号携带过去
                busInfo.setClassNum(classInfos.get(popSelectedPos).getClassnum());
                EventBus.getDefault().post(busInfo);
            }
        });
    }


    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {

        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.iv_choose_class) {
            //选择班级
            //选择班级
            if (isLoadClassCompleted) {
                //弹出选择班级的PopUpWindow
                showClassPop();
            } else {
                //没加载完成
                SnackbarUtil.ShortSnackbar(llBaseMain, Constants.LOADING, 0).show();
            }

        } else if (id == R.id.tv_cwdj) {
            //宠物等级
            llContent.setBackgroundResource(R.drawable.teacher_pet_level_bg);
            vp.setCurrentItem(0);
        } else if (id == R.id.tv_jyz) {
            //经验值
            llContent.setBackgroundResource(R.drawable.teacher_jyz_bg);
            vp.setCurrentItem(1);
        } else if (id == R.id.tv_jb) {
            //金币
            llContent.setBackgroundResource(R.drawable.teacher_gold_bg);
            vp.setCurrentItem(2);
        } else if (id == R.id.tv_dk) {
            //打卡
            llContent.setBackgroundResource(R.drawable.teacher_push_card_bg);
            vp.setCurrentItem(3);
        }
    }
}
